package com.projects.android.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.projects.android.R;
import com.projects.android.bus.UsersBUS;
import com.projects.android.dto.UsersDTO;
import com.projects.android.utils.SelectedItems;
import com.projects.android.utils.Utils;

public class Register extends Activity implements OnClickListener {
	private Intent intent;
	private Button btnCancel;
	private Button btnOK;

	private EditText txtUserName;
	private EditText txtPassword;
	private EditText txtConfirmPassword;
	private EditText txtEmail;
	private EditText txtName;
	private EditText txtPhone;
	private EditText txtAddress;

	// Use an existing ListAdapter that will map an array
	// of strings to TextViews
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initFrm();
	}

	private void initFrm() {
		btnCancel = (Button) findViewById(R.id.btnRegisterCancel);
		btnOK = (Button) findViewById(R.id.btnRegisterOK);

		txtUserName = (EditText) findViewById(R.id.txtRegisterUserName);
		txtPassword = (EditText) findViewById(R.id.txtRegisterPassword);
		txtConfirmPassword = (EditText) findViewById(R.id.txtRegisterComfirmPassword);
		txtEmail = (EditText) findViewById(R.id.txtRegisterEmail);
		txtName = (EditText) findViewById(R.id.txtRegisterName);
		txtPhone = (EditText) findViewById(R.id.txtRegisterPhone);
		txtAddress = (EditText) findViewById(R.id.txtRegisterAddress);

		txtPassword.setTransformationMethod(PasswordTransformationMethod
				.getInstance());
		txtConfirmPassword.setTransformationMethod(PasswordTransformationMethod
				.getInstance());

		btnOK.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnRegisterCancel:
			intent = Utils.browseIntent(this, "Intro");
			startActivity(intent);
			break;
		case R.id.btnRegisterOK:
			if (!ValidateRegister()) {
				return;
			}
			final ProgressDialog dialog = new ProgressDialog(this);
			dialog.setTitle("Loading. Please wait...");
			dialog.setIndeterminate(true);

			final Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {
						String result = RegisterMember();

						@Override
						public void run() {
							dialog.dismiss();
							if (result.equals("")) {
								AlertDialog dlg = new AlertDialog.Builder(Register.this)
										.setTitle("Register")
										.setMessage("Register successful")
										.setPositiveButton(
												"OK",
												new DialogInterface.OnClickListener() {
													public void onClick(
															DialogInterface dialog,
															int whichButton) {
														intent = Utils
																.browseIntent(
																		Register.this,
																		"Intro");
														startActivity(intent);
													}
												}).create();
								dlg.show();
							} else {
								AlertDialog dlg = Utils.showAlertDlg(
										Register.this, "Register", result);
								dlg.show();
							}
						}
					});
				}
			});
			dialog.show();
			thread.start();
			break;
		}
	}

	public String RegisterMember() {
		// Register member
		UsersDTO newUser = new UsersDTO();
		newUser.setUserName(txtUserName.getText().toString());
		newUser.setPassword(txtPassword.getText().toString());
		newUser.setEmail(txtEmail.getText().toString());
		newUser.setName(txtName.getText().toString());
		newUser.setPhone(txtPhone.getText().toString());
		newUser.setAddress(txtAddress.getText().toString());

		String result = UsersBUS.insert(newUser);
		if (!result.equals("")) {
			return result;
		}

		SelectedItems.SelectedUsers = UsersBUS.selectByUserName(newUser
				.getUserName());
		return "";
	}

	public boolean ValidateRegister() {
		String errorTxt = "";
		if (txtUserName.getText().toString().equals("")) {
			errorTxt = "UserName is empty";
		} else if (txtPassword.getText().toString().equals("")) {
			errorTxt = "Password is empty";
		} else if (txtConfirmPassword.getText().toString().equals("")) {
			errorTxt = "Confirm password is empty";
		} else if (!txtPassword.getText().toString()
				.equals(txtConfirmPassword.getText().toString())) {
			errorTxt = "Password and confirm is not match";
		} else if (txtEmail.getText().toString().equals("")) {
			errorTxt = "Email is empty";
		} else if (txtName.getText().toString().equals("")) {
			errorTxt = "Name is empty";
		} else if (txtPhone.getText().toString().equals("")) {
			errorTxt = "Phone is empty";
		} else if (txtAddress.getText().toString().equals("")) {
			errorTxt = "Address is empty";
		}
		if (!errorTxt.equals("")) {
			AlertDialog dlg = Utils.showAlertDlg(this, "Validate Register",
					errorTxt);
			dlg.show();
			return false;
		}
		return true;
	}
}
